//
//  NgHomepageViewController.m
//  NgTopOSShove
//
//  Created by yscompany on 2018/11/20.
//  Copyright © 2018 NgTopOS. All rights reserved.
//

#import "NgHomepageViewController.h"
#import "NgSliderView.h"
#import "NgListViewController.h"
#import "NgCollectionHeadView.h"

@interface NgHomepageViewController ()<UIScrollViewDelegate, NgSliderViewDelegate>
@property (nonatomic, strong) NgSliderView *sliderView;
@property (nonatomic, strong) UIScrollView *baseScrollerView;
@property (nonatomic, strong) NSArray *itemArray; //数据源
@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, strong) NSArray *sliderInfoDictArray;
@property (nonatomic, strong) NgCollectionHeadView *headView;
@end

@implementation NgHomepageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.sliderView];
    [self.view addSubview:self.headView];
    self.baseScrollerView.frame = CGRectMake(0, CGRectGetMaxY(self.headView.frame), kScreenW, kScreenH);
    [self setChildInfo];
    [self.view insertSubview:self.baseScrollerView atIndex:0];
    [self scrollViewDidEndScrollingAnimation:self.baseScrollerView];
    
    self.currentIndex = 0;
    
    
    // Do any additional setup after loading the view.
}
#pragma mark - private UI method
- (void)setChildInfo {
    for (NSString * title in self.itemArray) {
        NgListViewController *vc = [NgListViewController new];
        vc.title = [HDVerfiyTools getSafeString:title];
        [self addChildViewController:vc];
    }
}
#pragma mark - scrollview delegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.baseScrollerView) {
        [self scrollViewDidEndScrollingAnimation:scrollView];
        NSInteger index = scrollView.contentOffset.x/self.baseScrollerView.frame.size.width;
        [self.sliderView getSliderPageIndex:index];
    }
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    if (scrollView == self.baseScrollerView) {
        NSInteger index = scrollView.contentOffset.x/self.baseScrollerView.frame.size.width;
        NgListViewController *vc = self.childViewControllers[index];
        vc.view.frame = CGRectMake(scrollView.contentOffset.x, 0, self.baseScrollerView.frame.size.width, self.baseScrollerView.frame.size.height);
//        vc.tableView.contentInset = UIEdgeInsetsMake(0, 0, self.tabBarController.tabBar.frame.size.height, 0);
        [scrollView addSubview:vc.view];
//        for (int i = 0; i<self.baseScrollerView.subviews.count; i++) {
//            NSInteger currentIndex = vc.tableView.frame.origin.x/self.baseScrollerView.frame.size.width;
//            if ([self.baseScrollerView.subviews[i] isKindOfClass:[UITableView class]]) {
//                UITableView *theTableView = self.baseScrollerView.subviews[i];
//                NSInteger theIndex = theTableView.frame.origin.x/self.baseScrollerView.frame.size.width;
//                NSInteger gap = theIndex - currentIndex;
//                if (gap<=2&&gap>=-2) {
//                    continue;
//                } else {
//                    [theTableView removeFromSuperview];
//                }
//            }
//            
//        }
        
    }
}
#pragma mark - nssliderDelegate
- (void)setSelectIndex:(NSInteger)index {
    NgListViewController *vc = self.childViewControllers[index];
    self.baseScrollerView.contentOffset = CGPointMake(index * kScreenW, 0);
    vc.view.frame = CGRectMake(self.baseScrollerView.contentOffset.x, 0, self.baseScrollerView.frame.size.width, self.baseScrollerView.frame.size.height);
    //        vc.tableView.contentInset = UIEdgeInsetsMake(0, 0, self.tabBarController.tabBar.frame.size.height, 0);
    [self.baseScrollerView addSubview:vc.view];
}
#pragma mark - getter method
- (NgSliderView *)sliderView {
    if (!_sliderView) {
        _sliderView = [[NgSliderView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.navigationController.tabBarController.tabBar.frame), kScreenW, 60)];
        _sliderView.itemsArray = self.itemArray;
        _sliderView.delegate = self;
    }
    return _sliderView;
}
- (UIScrollView *)baseScrollerView {
    if (!_baseScrollerView) {
        _baseScrollerView = [UIScrollView new];
        _baseScrollerView.backgroundColor = [UIColor whiteColor];
        _baseScrollerView.showsHorizontalScrollIndicator = NO;
        _baseScrollerView.showsVerticalScrollIndicator = NO;
        _baseScrollerView.delegate = self;
        _baseScrollerView.pagingEnabled = YES;
        _baseScrollerView.contentSize = CGSizeMake(self.itemArray.count * kScreenW, 0);
    }
    return _baseScrollerView;
}
- (NSArray *)itemArray {
    if (!_itemArray) {
        _itemArray = @[@"衣服",@"鞋子",@"裤子",@"母婴",@"母婴",@"母婴",@"母婴",@"母婴",@"母婴",@"母婴",@"母婴",@"母婴"];
    }
    return _itemArray;
}
- (NSArray *)sliderInfoDictArray {
    if (!_sliderInfoDictArray) {
        _sliderInfoDictArray = @[@{@"imgUrl":@"https://goss.veer.com/creative/vcg/veer/800water/veer-133101916.jpg",@"url":@"www.baidu.com"},@{@"imgUrl":@"https://goss.veer.com/creative/vcg/veer/800water/veer-133101912.jpg",@"url":@"www.163.com"},@{@"imgUrl":@"https://goss.veer.com/creative/vcg/veer/800water/veer-133101913.jpg",@"url":@"www.sohu.com"},@{@"imgUrl":@"https://goss.veer.com/creative/vcg/veer/800water/veer-133101914.jpg",@"url":@"www.sogou.com"},@{@"imgUrl":@"https://goss.veer.com/creative/vcg/veer/800water/veer-133101915.jpg",@"url":@"www.wangyi.com"}];
    }
    return _sliderInfoDictArray;
}
- (NgCollectionHeadView *)headView {
    if (!_headView) {
        _headView = [[NgCollectionHeadView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.sliderView.frame), kScreenW, 120) collectionType:NgCollectionTypeSliderCarousel infoDictArray:self.sliderInfoDictArray];
    }
    return _headView;
}

@end
